package edu.purdue.cs505;

public class Sender
{

  private static String SENDER_IP = "128.10.25.110";
  private static String RECEIVER_IP = "128.10.25.111";
  static int senderPort = 8001;
  static int receiverPort = 8002;
   static ReliableChannelImpl sender;

   public static void main(String[] args)
   {
      sender = new ReliableChannelImpl(SENDER_IP, senderPort);
      sender.init(RECEIVER_IP, receiverPort);
      try {Thread.sleep(5000);} catch (InterruptedException e) {}
      sendMessage(sender, "simple test from A", false);
      sender.halt();
   }

   private static void simpleTest()
   {
      sendMessage(sender, "simple test from A", false);

      try
      {
         Thread.sleep(1000);
      }
      catch (InterruptedException e)
      {
         e.printStackTrace();
      }
   }

   private static void resendTest()
   {
      sendMessage(sender, "resend test from A", true);

      try
      {
         Thread.sleep(6000);
      }
      catch (InterruptedException e)
      {
         e.printStackTrace();
      }
   }

   private static void manyMessagesTest()
   {
      for (int i = 0; i < 100; i++)
      {
         sendMessage(sender, "from A " + i, false);
      }

      try
      {
         Thread.sleep(10000);
      }
      catch (InterruptedException e)
      {
         e.printStackTrace();
      }
   }

   private static void sendMessage(ReliableChannelImpl nodeFrom, String message, boolean fail)
   {
      Message m = new Message();
      m.setContents(message);
      if (!fail)
         nodeFrom.rsend(m);
      else
         nodeFrom.rsendFail(m);
   }

}
